package com.water.wfo.component.api.domain;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;

/**
 * packageName com.water.wfo.component.api.domain
 *
 * @author
 * @className CookieToken(处以class为网)
 * @date 2025/1/17
 * @description T0D0
 */
@Slf4j
public class CookieToken extends Token{
    private String empCode;
    private String deptName;
    private String deptCode;
    private Long expireTime = System.currentTimeMillis();

    public CookieToken(String sessionId,String userId, String empCode, String deptCode, int expireSecond) {
        this.setUserId(userId);
        this.setSessionId(sessionId);
        this.empCode = empCode;
        this.deptName = deptName;
        this.deptCode = deptCode;
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(System.currentTimeMillis());
        int millisecond = expireSecond * 1000;
        c.add(14, millisecond);
        this.expireTime = c.getTimeInMillis();
    }

    public CookieToken(byte[] tokenBytes) {
        try {
            String token = new String(tokenBytes, "UTF-8");
            String[] keys = token.split(",");
            this.setSessionId(keys[0]);
            this.setUserId(keys[1]);
            this.empCode = keys[2];
            this.deptCode = keys[3];
            if (keys.length <= 5) {
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(System.currentTimeMillis());
                int millisecond = 3600000;
                c.add(14, millisecond);
                this.expireTime = c.getTimeInMillis();
            } else {
                this.deptName = keys[4];

                try {
                    this.expireTime = Long.parseLong(keys[5]);
                } catch (Exception var7) {
                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(System.currentTimeMillis());
                    int millisecond = 3600000;
                    c.add(14, millisecond);
                    this.expireTime = c.getTimeInMillis();
                }
            }
        } catch (UnsupportedEncodingException var8) {
            log.error(var8.getMessage(), var8);
        }

    }

    public byte[] toBytes() {
        try {
            return this.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException var2) {
            log.error(var2.getMessage(), var2);
            return null;
        }
    }

    public String toString() {
        super.toString();
        StringBuffer sb = new StringBuffer(8);
        sb.append(this.getSessionId()).append(",");
        sb.append(this.getUserId()).append(",");
        sb.append(this.getEmpCode()).append(",");
        sb.append(this.getDeptCode()).append(",");
        sb.append(this.getDeptName()).append(",");
        sb.append(this.getExpireTime());
        return sb.toString();
    }

    public boolean expired() {
        long millisecond = this.getExpireTime();
        long currentMs = System.currentTimeMillis();
        return millisecond < currentMs;
    }
    public String getDeptCode() {
        return deptCode;
    }

    public void setDeptCode(String deptCode) {
        this.deptCode = deptCode;
    }

    public String getEmpCode() {
        return empCode;
    }

    public void setEmpCode(String empCode) {
        this.empCode = empCode;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    @Override
    public Long getExpireTime() {
        return expireTime;
    }

    @Override
    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }
}
